<?php

namespace App\Console\Commands;

use App\Facades\CommonPlatformRequestFacade;
use App\Models\UserBankCard;
use Illuminate\Console\Command;

class RepairBankCardCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'repair_bank_card';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '2048M');
        set_time_limit(0);
        $cards = UserBankCard::where('is_credit',3)->get();
        foreach ($cards as $card){
            $ret = CommonPlatformRequestFacade::bankCode($card->card_num);
            if($ret['code'] == '01'){
                if($ret['data']['is_credit'] == 'Y'){
                    UserBankCard::where('id',$card->id)->update([
                        'is_credit' => 1,
                        'bank_name' => $ret['data']['bank_name'],
                        'bank_code' => $ret['data']['bank_code'],
                        'bank_name_show' => $ret['data']['bank_name_show']
                    ]);
                    $this->info('用户'.$card->uid.'信用卡修复完成！');
                }else{
                    UserBankCard::where('id',$card->id)->update([
                        'is_credit' => 2,
                        'bank_name' => $ret['data']['bank_name'],
                        'bank_code' => $ret['data']['bank_code'],
                        'bank_name_show' => $ret['data']['bank_name_show']
                    ]);
                    $this->info('用户'.$card->uid.'储蓄卡修复完成！');
                }
            }else{
                if(!$card->bank_name && $card->bank_name_show){
                    UserBankCard::where('id',$card->id)->update([
                        'bank_name' => $card->bank_name_show
                    ]);
                    $this->info('用户'.$card->uid.'银行卡修复完成！');
                }
            }
        }
        //
    }
}
